package com.superpai.view.adapter;

import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.view.View;
import android.widget.Gallery;
import android.widget.ImageView;

import com.superpai.R;
import com.superpai.utils.BitmapUtils;
import com.superpai.view.download.ILoadImageListener;
import com.superpai.view.download.ImageLoadAdapter;

/**
 * @author Devin.Hu
 * @date 2011-9-17
 * @version V1.0
 * @description 产品详情adapter
 */
public class ProductDetailAdapter extends ImageLoadAdapter implements ILoadImageListener{

	private Context mContext;
	private int mItemWidth, mItemHeight;
	private List<String> imgList;
	
	public List<String> getImgList() {
		return imgList;
	}

	public void setImgList(List<String> imgList) {
		this.imgList = imgList;
		setDataCount(this.imgList.size());
	}

	public ProductDetailAdapter(Context context) {
		super(context);
		this.mContext = context;
		this.setLoadImageListener(this);
		mItemWidth = (int)context.getResources().getDimension(R.dimen.gallery_item_width);
		mItemHeight = (int)context.getResources().getDimension(R.dimen.gallery_item_height);
	}

	@Override
	public View getCacheDefaultView(int position) {
		ImageView imageView = new ImageView(mContext);
		Gallery.LayoutParams params = new Gallery.LayoutParams(mItemWidth, mItemHeight);
		imageView.setLayoutParams(params);
		imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
		imageView.setImageResource(R.drawable.product_default);
		BitmapDrawable drawable= (BitmapDrawable)imageView.getDrawable();
		drawable.setAntiAlias(true);
		return imageView;
	}

	@Override
	public void processDefaultState(View view, int position) {
		ImageView imageView = (ImageView)view;
		imageView.setImageResource(R.drawable.product_default);
	}

	@Override
	public String getDownloadUrl(int position) {
		return imgList.get(position);
	}

	@Override
	public void processEmptyState(View view, int position) {
		ImageView imageView = (ImageView)view;
		imageView.setImageResource(R.drawable.product_default);
	}

	@Override
	public void processErrorState(View view, int position) {
		ImageView imageView = (ImageView)view;
		imageView.setImageResource(R.drawable.product_default);
	}

	@Override
	public Bitmap processDownloadBytes(View view, byte[] bytes) {
		
		Bitmap bmp = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
		
		if(bmp!=null){
			float scale = view.getHeight()/(float)bmp.getHeight();
			Bitmap reflectedBmp = BitmapUtils.createReflectedImage(bmp, 0.1f, scale);
			bmp.recycle();
			return reflectedBmp;
		}
		return null;
	}

	@Override
	public void processDownloadBmp(View view, Bitmap bmp, int position) {
		if(!mIsDestroy){
			ImageView imageView = (ImageView)view;
			imageView.setImageBitmap(bmp);
			BitmapDrawable drawable = (BitmapDrawable)imageView.getDrawable();
			drawable.setAntiAlias(true);
		}else{
			bmp.recycle();
		}
	}

	@Override
	public void destroy() {
		super.destroy();
		setDataCount(0);
		if(imgList!=null){
			imgList.clear();
			imgList = null;
		}
		notifyDataSetInvalidated();
		mContext = null;
	}
}
